package com.elong.businesstravel.modules.member;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.android.support.jhf.debug.DebugLog;
import com.android.support.jhf.handlerui.HandlerPostUI;
import com.android.support.jhf.handlerui.HandlerToastUI;
import com.android.support.jhf.network.NetworkClient;
import com.android.support.jhf.network.loopj.android.http.ResponseHandlerInterface;
import com.elong.businesstravel.R;
import com.elong.businesstravel.base.activity.BaseActivity;
import com.elong.businesstravel.base.view.TitleNavBarView;
import com.elong.businesstravel.entitys.MemberManaged;
import com.elong.businesstravel.protocol.login.MemberGetManagedRequest;
import com.elong.businesstravel.protocol.login.MemberGetManagedResponse;

/**
 * 企业认证
 * 
 * @author lenovo
 * 
 */
public class EnterpriseCertificationActivity extends BaseActivity {

	private TextView mAuthenticateTextView;
	private ViewGroup validateLayout;
	private ViewGroup companynameLayout;
	private ViewGroup showmsgLayout;
	private TextView companynameTextView;
	private TextView showmsgTextView;

	private MemberManaged mMemberManaged;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_enterprise_certification);

		init();
	}

	protected void init() {

		mAuthenticateTextView = (TextView) findViewById(R.id.authenticateTextView);

		validateLayout = (ViewGroup) findViewById(R.id.validateLayout);

		companynameLayout = (ViewGroup) findViewById(R.id.companynameLayout);
		showmsgLayout = (ViewGroup) findViewById(R.id.showmsgLayout);

		companynameTextView = (TextView) findViewById(R.id.companynameTextView);
		showmsgTextView = (TextView) findViewById(R.id.showmsgTextView);

		setData();

		setTitleNavBar();
		setListener();

		memberGetManaged();
	}

	private void setData() {
		if (null != mMemberManaged && mMemberManaged.isValidateEmail()) {
			mAuthenticateTextView.setText(mMemberManaged.getValidateEmail());
			validateLayout.setVisibility(View.GONE);
			companynameLayout.setVisibility(View.VISIBLE);
			showmsgLayout.setVisibility(View.VISIBLE);
			companynameTextView.setText(mMemberManaged.companyname);
			showmsgTextView.setText(mMemberManaged.showmsg);
		} else {
			validateLayout.setVisibility(View.VISIBLE);
			companynameLayout.setVisibility(View.GONE);
			showmsgLayout.setVisibility(View.GONE);
		}
	}

	@Override
	protected void setListener() {

		ViewGroup emailAuthenticateLayout = (ViewGroup) findViewById(R.id.emailAuthenticateLayout);
		emailAuthenticateLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext,
						EmailAuthenticateActivity.class);
				startActivityForResult(intent, 0);
			}
		});
		ViewGroup cardAuthenticateLayout = (ViewGroup) findViewById(R.id.cardAuthenticateLayout);
		cardAuthenticateLayout.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mContext,
						CardAuthenticateActivity.class);
				startActivityForResult(intent, 0);
			}
		});
	}

	@Override
	protected void setTitleNavBar() {
		TitleNavBarView titleNavBarView = (TitleNavBarView) findViewById(R.id.titleNavBarView);
		titleNavBarView.setMessage("企业认证");
		titleNavBarView.setLeftDrawableImage(R.drawable.btn_title_back);
		titleNavBarView.setLeftButtonImageString(0, "返回", R.color.text_blue);
		titleNavBarView.setLeftButtonOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});
		titleNavBarView.setRightButtonVisibility(View.INVISIBLE);
	}

	private void memberGetManaged() {
		NetworkClient.getNetworkClient().PostRequest(
				new MemberGetManagedRequest(mContext),
				new MemberGetManagedResponse() {

					@Override
					public void onResponse(
							ResponseHandlerInterface responseHandlerInterface) {
						HandlerPostUI.getHandlerPostUI(new Runnable() {

							@Override
							public void run() {
								mMemberManaged = memberManaged;

								if (!"0".equals(mMemberManaged.status)) {
									if (!TextUtils.isEmpty(mMemberManaged.msg)) {
										HandlerToastUI.getHandlerToastUI(
												mContext, mMemberManaged.msg);
									}
								}
								
								HandlerPostUI.getHandlerPostUI(new Runnable() {
									
									@Override
									public void run() {
										setData();
									}
								});
							}
						});
					}

				});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		DebugLog.logI("requestCode : " + requestCode);
		DebugLog.logI("resultCode : " + resultCode);
		switch (requestCode) {
		case 0: {
			// 认证界面返回
			switch (resultCode) {
			case RESULT_OK: {
				memberGetManaged();
				break;
			}
			case RESULT_CANCELED: {
				break;
			}
			default:
				break;
			}
			break;
		}
		default:
			break;
		}
	}

}
